1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 import java.io.*;
32 import java.util.*;
33 import java.util.concurrent.*;
34 import java.util.concurrent.atomic.*;
35
36 public class Get {
37
38 private static void realMain(String[] args) throws Throwable {
39 testMap(new Hashtable<Character,Boolean>());
40 testMap(new HashMap<Character,Boolean>());
41 testMap(new IdentityHashMap<Character,Boolean>());
42 testMap(new LinkedHashMap<Character,Boolean>());
43 testMap(new ConcurrentHashMap<Character,Boolean>());
44 testMap(new WeakHashMap<Character,Boolean>());
45 testMap(new TreeMap<Character,Boolean>());
46 testMap(new ConcurrentSkipListMap<Character,Boolean>());
47 }
48
49 private static void put(Map<Character,Boolean> m,
50 Character key, Boolean value,
51 Boolean oldValue) {
52 if (oldValue != null) {
53 check(m.containsValue(oldValue));
54 check(m.values().contains(oldValue));
55 }
56 equal(m.put(key, value), oldValue);
57 equal(m.get(key), value);
58 check(m.containsKey(key));
59 check(m.keySet().contains(key));
60 check(m.containsValue(value));
61 check(m.values().contains(value));
62 check(! m.isEmpty());
63 }
64
65 private static void testMap(Map<Character,Boolean> m) {
66
67 boolean permitsNullKeys = (! (m instanceof ConcurrentMap ||
68 m instanceof Hashtable ||
69 m instanceof SortedMap));
70 boolean permitsNullValues = (! (m instanceof ConcurrentMap ||
71 m instanceof Hashtable));
72 boolean usesIdentity = m instanceof IdentityHashMap;
73
74 System.out.println(m.getClass());
75 put(m, 'A', true, null);
76 put(m, 'A', false, true);
77 put(m, 'B', true, null);
78 put(m, new Character('A'), false, usesIdentity ? null : false);
79 if (permitsNullKeys) {
80 try {
81 put(m, null, true, null);
82 put(m, null, false, true);
83 }
84 catch (Throwable t) { unexpected(t); }
85 } else {
86 try { m.get(null); fail(); }
87 catch (NullPointerException e) {}
88 catch (Throwable t) { unexpected(t); }
89
90 try { m.put(null, true); fail(); }
91 catch (NullPointerException e) {}
92 catch (Throwable t) { unexpected(t); }
93 }
94 if (permitsNullValues) {
95 try {
96 put(m, 'C', null, null);
97 put(m, 'C', true, null);
98 put(m, 'C', null, true);
99 }
100 catch (Throwable t) { unexpected(t); }
101 } else {
102 try { m.put('A', null); fail(); }
103 catch (NullPointerException e) {}
104 catch (Throwable t) { unexpected(t); }
105
106 try { m.put('C', null); fail(); }
107 catch (NullPointerException e) {}
108 catch (Throwable t) { unexpected(t); }
109 }
110 }
111
112
113 static volatile int passed = 0, failed = 0;
114 static void pass() { passed++; }
115 static void fail() { failed++; Thread.dumpStack(); }
116 static void fail(String msg) { System.out.println(msg); fail(); }
117 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
118 static void check(boolean cond) { if (cond) pass(); else fail(); }
119 static void equal(Object x, Object y) {
120 if (x == null ? y == null : x.equals(y)) pass();
121 else {System.out.println(x + " not equal to " + y); fail(); }}
122
123 public static void main(String[] args) throws Throwable {
124 try { realMain(args); } catch (Throwable t) { unexpected(t); }
125
126 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
127 if (failed > 0) throw new Exception("Some tests failed");
128 }
129 }